package com.atguigu.guliedu.edu.controller;


import com.atguigu.common.utils.R;
import com.atguigu.guliedu.edu.entity.Subject;
import com.atguigu.guliedu.edu.entity.vo.SubjectVo;
import com.atguigu.guliedu.edu.service.SubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程分类前端控制器
 * </p>
 *
 * @author java
 * @since 2021-02-01
 */
@RestController
@RequestMapping("/edu/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    /**
     * 获取分类列表
     * @param type
     * @return
     */
    @GetMapping("get/{type}")
    public R list(@PathVariable String type){

        if("list".equals(type)){
            List<Subject> subjects = subjectService.list();
            return R.ok().data("data",subjects);
        }else{
            List<SubjectVo> list = subjectService.tree();
            return R.ok().data("data",list);
        }
    }

    /**
     * 获取一级分类
     * @return
     */
    @GetMapping("type/1")
    public R getLevel1List(){
        List<Subject> list = subjectService.list(new QueryWrapper<Subject>().eq("parent_id", '0'));
        return R.ok().data("data",list);
    }

    /**
     * 保存
     * @param subject
     * @return
     */
    @PutMapping("save")
    public R save(@RequestBody Subject subject){
        subject.setGmtModified(new Date());
        subject.setGmtCreate(new Date());
        subjectService.save(subject);
        return R.ok();
    }

    /**
     * 更新
     * @param subject
     * @return
     */
    @PostMapping("update")
    public R update(@RequestBody Subject subject){

        subject.setGmtModified(new Date());
        subjectService.updateById(subject);
        return R.ok();
    }
}

